package com.leyou.user.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.auth.mapper.CategoryMapper;
import com.leyou.search.pojo.Category;
import com.leyou.search.pojo.CategoryDTO;
import com.leyou.service.IcategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.List;

@Service
public class categoryServiceImpl implements IcategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryDTO> queryListByParent(Long pid) {
        Category category = new Category();
        category.setParentId(pid);
        List<Category> categorys = categoryMapper.select(category);
        if (CollectionUtils.isEmpty(categorys)){
            throw  new LyException(ExceptionEnum.CATEGORY_NOT_FOND);
        }
        return BeanHelper.copyWithCollection(categorys,CategoryDTO.class);
    }

    @Override
    public List<CategoryDTO> queryByBrandId(Long bid) {
       List<Category> list = categoryMapper.queryByBrandId(bid);

       if (CollectionUtils.isEmpty(list)){
           throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
       }
        List<CategoryDTO> categoryDTOS = BeanHelper.copyWithCollection(list, CategoryDTO.class);

        return categoryDTOS;
    }

    @Override
    public List<CategoryDTO> queryCategoryByIds(List<Long> categoryIds) {
        List<Category> list = categoryMapper.selectByIdList(categoryIds);
        if (CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }
}
